// pages/payorder/payorder.js
const product = require('../../utils/product.js')
var app = getApp()
Page({
  data:{
        orderid:''
  },

  onLoad:function(options){
      var orderid=options.orderid
      this.setData({
          orderid:orderid
      })
  },
  onReady:function(options){
      var that = this
      var info = getApp().data.info
      var sumPrice = getApp().data.info.price * getApp().data.info.buyNumber
      that.setData({ 'info': info })
      that.setData({ 'sumPrice': sumPrice })
      console.log("info",info)
      try {
          var value = wx.getStorageSync('infodata')
          that.setData({ 'value': value })
          console.log("value",value)
          if (value) {
              // Do something with return value
          }
      } catch (e) {
          // Do something when catch error
      }
  },
  wxpay: function () {         //微信支付
        
        this.generateOrder()
    },

 
    /**生成商户订单 */
    generateOrder: function (order_id) {
        var that = this
        //统一支付
        var orderid=this.data.orderid
          var session_3rd = wx.getStorageSync('session_3rd')
          var mpid=getApp().data.mp_id
        wx.request({
            url: `${app.globalData.DOMAIN}/mpbase/wxapp/wxpay/mp_id/`+mpid+'/id/' + orderid,
            method: 'post',
            data: {
                session_3rd:session_3rd
            },
            success: function (res) {
                console.log(res)
                console.log(typeof res.data)
                 console.log(JSON.parse(res.data))

                var pay = JSON.parse(res.data)
                console.log(pay)
                //发起支付
                var timeStamp = pay.timeStamp;
                console.log("timeStamp:" + timeStamp)
                var packages = pay.package;
                console.log("package:" + packages)
                var paySign = pay.paySign;
                console.log("paySign:" + paySign)
                var nonceStr = pay.nonceStr;
                console.log("nonceStr:" + nonceStr)
                var param = { "timeStamp": timeStamp, "package": packages, "paySign": paySign, "signType": "MD5", "nonceStr": nonceStr };
                that.pay(param)
            },
        })
    },

    /* 支付   */
    pay: function (param) {
        console.log("支付")
        console.log(param)
        wx.requestPayment({
            timeStamp: param.timeStamp,
            nonceStr: param.nonceStr,
            package: param.package,
            signType: param.signType,
            paySign: param.paySign,
            success: function (res) {
                // success
                console.log("支付")
                console.log(res)
                wx.navigateBack({
                    delta: 1, // 回退前 delta(默认为1) 页面
                    success: function (res) {
                        wx.showToast({
                            title: '支付成功',
                            icon: 'success',
                            duration: 2000
                        })
                    },
                    fail: function () {
                        // fail

                    },
                    complete: function () {
                        // complete
                    }
                })
            },
            fail: function (res) {
                // fail
                console.log("支付失败")
                console.log(res)
                 wx.showModal({ title: '提示', content: '支付失败', })
            return

            },
            complete: function () {
                // complete
                console.log("pay complete")
            }
        })
    },





})